﻿using Microsoft.AspNetCore.Mvc;
using tools.algorithm;

namespace tools.api.Controllers
{
    /// <summary>
    /// XXHash 工具操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class XxHashController : ControllerBase
    {
        private const long _fileSizeLimit = 1024 * 1024 * 500; // 500MB

        [HttpGet]
        public IActionResult Compute_Strig_64(string text)
        {
            return Ok(XxHashTool.Get64(text));
        }

        [HttpGet]
        public IActionResult Compute_Strig_128(string text)
        {
            return Ok(XxHashTool.Get128(text).ToString());
        }

        [HttpPost]
        [RequestSizeLimit(_fileSizeLimit)] // 提升接口上传文件大小限制
        public async Task<IActionResult> Compute_File_64(IFormFile formFile)
        {
            await using var stream = formFile.OpenReadStream();
            return Ok(XxHashTool.Get64(stream));
        }

        [HttpPost]
        [RequestSizeLimit(_fileSizeLimit)]
        public async Task<IActionResult> Compute_File_128(IFormFile formFile)
        {
            await using var stream = formFile.OpenReadStream();
            return Ok(XxHashTool.Get128(stream).ToString());
        }
    }
}
